// Copyright (c) 2007, Brian Duff

// See LICENSE for full license details.

package org.dubh.jdant;

import java.io.File;

import java.util.Collections;
import java.util.List;

/**
 * Represents a JDeveloper library. A library is a source of external jar files
 * used to compile or run a project.<p>
 * 
 * To create an instance of this object,  use the {@link Builder} nested 
 * class. For example:
 * 
 * <pre>
 *   Library lib = new Library.Builder( name, cp ).exported( true ).build();
 * </pre>
 *
 * Instances of this object are <i>immutable</i>.
 *
 * @author brian.duff@dubh.org
 */
// @Immutable
public final class Library
{
  private final String name;
  private final List<File> classpath;
  private final List<File> sourcepath;
  private final boolean exported;
  private final LibraryType type;
  
  private Library( Builder builder )
  {
    this.name = builder.name;
    this.classpath = Collections.unmodifiableList( builder.classpath );
    this.sourcepath = Collections.unmodifiableList( builder.sourcepath );
    this.exported = builder.exported;
    this.type = builder.type;
  }
  
  /**
   * Returns the name or identifier of this library.
   * 
   * @return the name or identifier of this library.
   */
  public String name()
  {
    return name;
  }
  
  /**
   * Returns the classpath of this library.
   * 
   * @return a collection of classpath entires. This method will never
   *  return null, although it may return an empty collection.
   */
  // @NotNull
  public List<File> classpath()
  {
    return classpath;
  }
  
  /**
   * Returns the sourcepath of this library.
   * 
   * @return a collection of sourcepath entries. This method will never 
   *    return null, although it may return an empty collection.
   */
  // @NotNull
  public List<File> sourcepath()
  {
    return sourcepath;
  }
  
  /**
   * Returns whether this library is exported. An exported library is visible
   * to projects that depend on the project using this library.
   * 
   * @return <tt>true</tt> if this library is exported.
   */
  public boolean exported()
  {
    return exported;
  }

  /**
   * Returns the type of the library.
   * 
   * @return the type of the library.
   */
  public LibraryType type()
  {
    return type;
  }
  
  @Override
  public String toString()
  {
    return String.format( 
      "Library[ name=%s, classpath=%s, sourcepath=%s, type=%s ]",
      name, classpath, sourcepath, type
    );
  }
  
  /**
   * Use the <tt>Builder</tt> to create instances of <tt>Library</tt>.
   */
  public static class Builder 
  {
    private final String name;
    private final List<File> classpath;

    private List<File> sourcepath = Collections.emptyList();    
    private boolean exported = false;
    private LibraryType type = LibraryType.EXTENSION;
    
    public Builder( String name, List<File> classpath )
    {
      this.name = name;
      this.classpath = classpath;
    }
    
    public Builder exported( boolean exported )
    {
      this.exported = exported;
      return this;
    }
    
    public Builder sourcepath( List<File> sourcepath )
    {
      this.sourcepath = sourcepath;
      return this;
    }
    
    public Builder type( LibraryType type )
    {
      this.type = type;
      return this;
    }
    
    public Library build()
    {
      return new Library( this );
    }
  }
}
